package com.qding.park.manager.timer.conf;


import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.impl.JedisClientSingle;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Service
@Scope("singleton")
public class JedisPoolConfigBean {

	private static final Logger logger = LoggerFactory.getLogger(JedisPoolConfigBean.class);

	@Autowired
	private RedisPoolConfig redisPoolConfig;

	@Bean(name = "jedisPoolConfig")
	public JedisPoolConfig jedisPoolConfig() {
		logger.debug("redisPoolConfig" + redisPoolConfig);
		logger.info("redisPoolConfig config ==> host:{}, port:{} ",redisPoolConfig.getHost(),redisPoolConfig.getPort());
		// 加载配置文件属性
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(redisPoolConfig.getMaxTotal());
		jedisPoolConfig.setMaxIdle(redisPoolConfig.getMaxIdle());
		jedisPoolConfig.setNumTestsPerEvictionRun(redisPoolConfig.getNumTestsPerEvictionRun());
		jedisPoolConfig.setTimeBetweenEvictionRunsMillis(redisPoolConfig.getTimeBetweenEvictionRunsMillis());
		jedisPoolConfig.setMinEvictableIdleTimeMillis(redisPoolConfig.getMinEvictableIdleTimeMillis());
		jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(redisPoolConfig.getSoftMinEvictableIdleTimeMillis());
		jedisPoolConfig.setMaxWaitMillis(redisPoolConfig.getMaxWaitMillis());
		jedisPoolConfig.setTestOnBorrow(redisPoolConfig.isTestOnBorrow());
		jedisPoolConfig.setTestWhileIdle(redisPoolConfig.isTestWhileIdle());
		jedisPoolConfig.setBlockWhenExhausted(redisPoolConfig.isBlockWhenExhausted());
		return jedisPoolConfig;
	}


	@Bean(name="jedisPool")
	public JedisPool jedisPool() throws RuntimeException {
		logger.debug("--> JedisPool");
		String host=redisPoolConfig.getHost();
		int port=redisPoolConfig.getPort();
		JedisPool  jedisPool=new JedisPool(jedisPoolConfig(),host,port);
		
		return jedisPool;
	}
	
	@Bean(name="jedisClient")
    public JedisClient jedisClient() {
        logger.debug("> JedisClient");
        JedisClientSingle  jedisClientSingle=new JedisClientSingle();
        jedisClientSingle.setJedisPool(jedisPool());
        return jedisClientSingle;
    }


    /**
      * postConstruct(这里用一句话描述这个方法的作用)
      * TODO(这里描述这个方法适用条件 – 可选)
      * TODO(这里描述这个方法的执行流程 – 可选)
      * TODO(这里描述这个方法的使用方法 – 可选)
      * TODO(这里描述这个方法的注意事项 – 可选)
      *
      * @Title: postConstruct
      * @Description:  通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
      * @param     设定文件
      * @return void    返回类型
      * @throws
      */
    @PostConstruct
    public void postConstruct() {
        logger.info("redisPoolConfig={}", redisPoolConfig);
    }
	
//	public Resource[] getResource(String basePackage, String pattern) throws IOException {
//		String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(new StandardEnvironment().resolveRequiredPlaceholders(basePackage)) + "/" + pattern;
//		Resource[] resources = new PathMatchingResourcePatternResolver().getResources(packageSearchPath);
//		return resources;
//	}
}
